package com.lims;

import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.CountDownLatch;

/**
 * @author limaosheng
 * @create 2024-08-05 10:52
 * @desc
 */
public class 多线程并发示例 {

    public static void main(String[] args) throws InterruptedException {
        Long start = System.currentTimeMillis();
        int threadNum = 100; // 假设我们有10个并发任务
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);

        for (int i = 0; i < threadNum; i++) {
            int taskId = i;
            ThreadUtil.execute(() -> {
                try {
                    // 模拟任务执行，这里只是简单打印
                    System.out.println("任务 " + taskId + " 开始执行");
                    Thread.sleep(500); // 模拟耗时操作
                    System.out.println("任务 " + taskId + " 执行完成");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    countDownLatch.countDown(); // 每个任务完成时，计数器减一
                }
            });
        }

        // 等待所有任务完成
        countDownLatch.await();
        System.out.println("所有任务执行完成");
    }
}
